#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   24. 两两交换链表中的节点.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/4 14:10
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""
from typing import Optional


# import lib
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        root = ListNode(-1, head)
        node = root
        while node and node.next and node.next.next:
            # 1
            nxt = node.next
            # 2
            nnxt = nxt.next
            # 3
            nnnxt = nnxt.next
            # -> 2
            node.next = nnxt
            node = node.next
            # -> 1
            node.next = nxt
            node = node.next
            # -> 3
            node.next = nnnxt
        return root.next
